package rgb.power.led.controller;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Twistx77
 */
public class CommunicationHandler {
    
    SerialHandler serialHandler;
    
    final static int FRAME_START = 0x7E;
    final static int HI_CMD = 0x01;
    final static int SET_OUTPUT_CMD = 0x02;
    final static int SET_PRESET_CMD = 0x03;
    final static int SET_IR_CMD = 0x04;
    final static int LENGTH_BYTE = 1;
    
    
    
    int bytesRead = 0;              //Number of bytes recieved

    public CommunicationHandler() {
        
        serialHandler = new SerialHandler(this);        
        
    }
    
    
    
    public String[] getAvailablePorts(){
        
       return (serialHandler.getAvailableSerialPorts());
       
    }
        
    
    public void connect(String portName, int baudRate){
        try {
            serialHandler.connect(portName, baudRate);
        } catch (Exception ex) {
            Logger.getLogger(CommunicationHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
     public void disconnect(){
        try {
            serialHandler.disconnect();
        } catch (Exception ex) {
            Logger.getLogger(CommunicationHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    public void sendSetOutputPacket(int R, int G,  int B){
        
        byte[] outputPacketBuffer = new byte[20];
        
        outputPacketBuffer[0] = FRAME_START;                             // Start of Packet
        outputPacketBuffer[1] = 9;                                       // Packet Size
        outputPacketBuffer[2] = SET_OUTPUT_CMD;                          // Set Output Command
        outputPacketBuffer[3] = (byte) ((R*16) >>> 8);                   // Red High Byte
        outputPacketBuffer[4] = (byte) (R*16);                           // Red Low Byte
        outputPacketBuffer[5] = (byte) ((G*16) >>> 8);                   // Green High Byte
        outputPacketBuffer[6] = (byte) (G*16);                           // Green Low Byte
        outputPacketBuffer[7] = (byte) ((B*16) >>> 8);                   // Blue High Byte
        outputPacketBuffer[8] = (byte) (B*16);                           // Blue Low Byte
        
        serialHandler.sendPacket(outputPacketBuffer);
        
    }
    
    public void sendPresetPacket(int presetNumber, int sel1R, int sel1B, int sel1G,int sel2R, int sel2G, int sel2B, int timeSel1, int timeOff, int timeSel2){
        
         byte[] outputPacketBuffer = new byte[20];
         
        outputPacketBuffer[0] = FRAME_START;                             // Start of Packet
        outputPacketBuffer[1] = 18;                                       // Packet Size
        outputPacketBuffer[2] = SET_PRESET_CMD;                          // Set Preset Command
        outputPacketBuffer[3] = (byte) presetNumber;                     // Preset Number        
        outputPacketBuffer[4] = (byte) ((sel1R*16) >>> 8);               // Selection 1 Red High Byte
        outputPacketBuffer[4] = (byte) (sel1R*16);                       // Selection 1 Red Low Byte
        outputPacketBuffer[5] = (byte) ((sel1G*16) >>> 8);               // Selection 1 Green High Byte
        outputPacketBuffer[6] = (byte) (sel1G*16);                       // Selection 1 Green Low Byte
        outputPacketBuffer[7] = (byte) ((sel1B*16) >>> 8);               // Selection 1 Blue High Byte
        outputPacketBuffer[8] = (byte) (sel1B*16);                       // Selection 1 Blue Low Byte
        outputPacketBuffer[9] = (byte)  ((sel2R*16) >>> 8);              // Selection 2 Red High Byte
        outputPacketBuffer[10] = (byte) (sel2R*16);                      // Selection 2 Red Low Byte
        outputPacketBuffer[11] = (byte) ((sel2G*16) >>> 8);              // Selection 2 Green High Byte
        outputPacketBuffer[12] = (byte) (sel2G*16);                      // Selection 2 Green Low Byte
        outputPacketBuffer[13] = (byte) ((sel2B*16) >>> 8);              // Selection 2 Blue High Byte
        outputPacketBuffer[14] = (byte) (sel2B*16);                      // Selection 2 Blue Low Byte
        outputPacketBuffer[15] = (byte) timeSel1;                        // Selection 1 time
        outputPacketBuffer[16] = (byte) timeOff;                         // Off time
        outputPacketBuffer[17] = (byte) timeSel2;                        // Selection 2 time
        
        
        serialHandler.sendPacket(outputPacketBuffer);
        
    }
    
   public void sendSetIRCode(int key){
     byte[] outputPacketBuffer = new byte[4];
     
        outputPacketBuffer[0] = FRAME_START;                             // Start of Packet
        outputPacketBuffer[1] = 18;                                       // Packet Size
        outputPacketBuffer[2] = SET_IR_CMD;                          // Set Preset Command
        outputPacketBuffer[3] = (byte) key;  
        
    serialHandler.sendPacket(outputPacketBuffer);
}
    
  
    
    public void recievePacket(int byteRecieved) {
        int[] inputPacketBuffer = new int[16];
        
        switch (bytesRead) {
            
            case 0:
                if (byteRecieved == FRAME_START) {
                    inputPacketBuffer[bytesRead] = byteRecieved;
                    bytesRead++;
                }
                
                break;
                
            case 1:
                inputPacketBuffer[bytesRead] = byteRecieved;
                bytesRead++;
                break;
                
            default:
                if (bytesRead < inputPacketBuffer[LENGTH_BYTE]){
                    inputPacketBuffer[bytesRead]= byteRecieved;
                    bytesRead++;
                }
                else {
                    bytesRead = 0;
                    processPacket(inputPacketBuffer);
                    
                }
                break;

        }
    }
    
    
    private void processPacket(int[] inputPacketBuffer){
        for (int i = 0; i<inputPacketBuffer[1]; i++)
           System.out.print("0x"+Integer.toHexString(inputPacketBuffer [i])+" ");       
    }
    
    
    
}
